package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.CompanyPurchaseProjects2Adapter
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.bean.old.AmibaInstallByIdBean
import com.hinnotek.amoeba.databinding.ActivityMsgPuachaseDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.NoticeViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MsgPurchaseDetailActivity : BaseActivity<ActivityMsgPuachaseDetailBinding>(),
    View.OnClickListener {

    private var adapter: CompanyPurchaseProjects2Adapter? = null
    private var companyPurchaseId = 0
    private var noticeId = 0
    private var applyUserId = 0
    private var approvalUserId: String? = null

    private val viewModel by viewModels<NoticeViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("采购")

        setContentView(R.layout.activity_msg_puachase_detail)
    }

    override fun initVew() {
        bindingView.superButtonHistory.setOnClickListener(this)
        bindingView.superButtonReject.setOnClickListener(this)
        bindingView.superButtonAgree.setOnClickListener(this)
        val manager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        bindingView.rlvProduct.layoutManager = manager
        bindingView.rlvProduct.adapter = CompanyPurchaseProjects2Adapter().also { adapter = it }
    }

    override fun setData() {
        val type = intent.getIntExtra("type", 0)
        noticeId = intent.getIntExtra("noticeId", 0)
        if (type == 0) {
            bindingView.llMsgBtns.visibility = View.VISIBLE
        } else if (type == 1) {
            bindingView.llMsgBtns.visibility = View.GONE
        } else {
            bindingView.superButtonReject.visibility = View.GONE
            bindingView.superButtonReject.visibility = View.GONE
        }
        getNoticeDetailById(noticeId)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.superButton_history -> {
                val bundle = Bundle()
                bundle.putInt("msgType", 6)
                bundle.putInt("applyUserId", applyUserId)
                bundle.putString("approvalUserId", approvalUserId)
                IntentUtils.startActivity(mContext, MsgHistoryListActivity::class.java, bundle)
            }
            R.id.superButton_reject -> companyPurchaseApproval(companyPurchaseId, 3, noticeId)
            R.id.superButton_agree -> companyPurchaseApproval(companyPurchaseId, 2, noticeId)
        }
    }

    /**
     * app端通过id查询单个详情接口
     */
    fun getNoticeDetailById(noticeId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        viewModel.getNoticeDetailById(noticeId).observe(this,object : ObserverState<AmibaInstallByIdBean>(){
            override fun onDataChange(data: AmibaInstallByIdBean) {
                applyUserId = data.applyUserId
                approvalUserId = data.approvalUserId
                companyPurchaseId = data.companyPurchaseId
                bindingView.tvItemPurchaseType.text = if (data.companyPurchaseType == 1) "产品" else "项目"
                bindingView.tvPurchaseAmoeba.text = data.orgName
                if (data.companyPurchaseType == 1) {
                      bindingView.tvPurchaseProject.text = data.provideProjectName
                } else {
                    bindingView.tvPurchaseCustomer.text = data.customerName
                    bindingView.tvPurchaseProject.text = data.projectName
                    bindingView.tvPurchaseCustomer.visibility = View.VISIBLE
                }
                bindingView.tvPurchaseMoney.text = data.purchaseMoney
                bindingView.tvPurchaseApprover.text = data.approvalUser
                adapter?.setList(data.amibaInstallContinueds)
                super.onDataChange(data)
            }

            override fun onError(msg: String?, code: Int) {
                super.onError(msg, code)
            }
        })
    }

    /**
     * app端添加临时缴费续费申请接口
     */
    private fun companyPurchaseApproval(
        companyPurchaseId: Int, approvalState: Int,
        noticeId: Int
    ) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        viewModel.getCompanyPurchaseApproval(companyPurchaseId,approvalState,noticeId).observe(this,object : ObserverState<BaseBean>(){
            override fun onDataChange(data: BaseBean) {
                customDialog.dismiss()
                finishActivity()
                super.onDataChange(data)
            }
            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                ToastUtils.showShort(mContext, msg)
                super.onError(msg, code)
            }
        })
    }
}